package com.insurance.controller;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.pojo.*;
import com.insurance.service.UserKFService;
import com.insurance.utils.ConversionDateUtils;
import com.insurance.utils.PasswordEncryption;
import com.insurance.utils.UploadFile;
import com.insurance.utils.UuidUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.*;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User1: 李辽
 * Date: 2020/10/28 15:08
 * Description:用户端的相关操作
 * Version: V1.0
 */
@Api(value = "用户端的相关操作",tags = "用户端的相关操作API")
@Controller
public class UserKFController {

    @Autowired
    private UserKFService userKFService;
    @ResponseBody
    @RequestMapping(value = "/queryInsuranceByUserId.do",method = RequestMethod.GET)
    @ApiOperation(value = "根据用户ID查询有无保单",notes = "根据用户ID查询有无保单接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="该用户有保单"),
            @ApiResponse(code=500,message="该用户暂无保单")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id",value = "用户id",required = true,dataType = "int",paramType = "query")
    })
    //根据用户ID查询有无保单
    public ResultData queryInsuranceByUserId(int user_id){
        ResultData resultData = new ResultData();
        //返回所有数据，可根据情况筛选
        List<Map> mapList = userKFService.queryInsuranceByUserId(user_id);
        if (mapList.size()>0){
            for (Map map:mapList) {
                if (map.get("high_ltime")!=null){

                map.put("high_ltime", ConversionDateUtils.conversionDateStringUtils(map.get("high_ltime")));
                }
                if (map.get("business_ltime")!=null) {
                    map.put("business_ltime", ConversionDateUtils.conversionDateStringUtils(map.get("business_ltime")));
                }
            }
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("该用户有保单");
            resultData.setData(mapList);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("该用户暂无保单");
        }
        return resultData;
    }


    @ResponseBody
    @RequestMapping(value = "/queryDriverByAreaId.do",method = RequestMethod.GET)
    @ApiOperation(value = "根据区域id查询司机，前面查询省市的接口",notes = "根据区域id查询司机，前面查询省市的接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="数据库没有此数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "area_city_id",value = "区域id",required = true,dataType = "int",paramType = "query")
    })
    //根据区域id查询司机，前面查询省市的接口
    public ResultData queryDriverByAreaId(int area_city_id){
        ResultData resultData = new ResultData();
        List<Map> mapList = userKFService.queryDriverByAreaId(area_city_id);
        if (mapList.size() > 0){
            resultData.setData(mapList);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("数据库没有此数据");
        }
        return resultData;
    }

    @ResponseBody
    @RequestMapping(value = "/query4SByAreaId.do",method = RequestMethod.GET)
    @ApiOperation(value = "根据区域id查询4s店，前面查询省市的接口",notes = "根据区域id查询4s店，前面查询省市的接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="数据库没有此数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "area_city_id",value = "区域id",required = true,dataType = "int",paramType = "query")
    })
    //根据区域id查询4s店，前面查询省市的接口
    public ResultData query4SByAreaId(int area_city_id){
        ResultData resultData = new ResultData();
        List<Map> mapList = userKFService.query4SByAreaId(area_city_id);
        if (mapList.size() > 0 ){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("4s店查询成功");
            resultData.setData(mapList);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("数据库没有此数据");
        }

        return resultData;
    }

    @ResponseBody
    @RequestMapping(value = "/queryOwnerCardByUserId.do",method = RequestMethod.GET)
    @ApiOperation(value = "根据用户id查询车主卡信息接口",notes = "根据用户id查询车主卡信息的接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    //根据用户id查询车主卡信息接口
    public ResultData queryOwnerCardByUserId(Integer userId){
        ResultData resultData =userKFService.queryOwnerCardByUserId(userId);
        return resultData;
    }
    /**
     * 增加订单信息的接口
     */
    @ResponseBody
    @RequestMapping(value = "/addOrder.do",method = RequestMethod.POST)
    @ApiOperation(value = "增加订单",notes = "增加订单信息的接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData addOrder(@RequestBody UserOrder userOrder )throws SQLException {
        System.out.println("================================="+userOrder);
        ResultData resultData =userKFService.addOrder(userOrder);
        return resultData;
    }
    @ResponseBody
    @RequestMapping(value = "/uploadImg.do",method = RequestMethod.POST)
    @ApiOperation(value = "上传图片附件",notes = "上传图片附件接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData uploadImg(@RequestParam MultipartFile[] file, HttpServletRequest request) throws IOException, FileUploadException {
        ResultData resultData=new ResultData();
        System.out.println(file.length);
        String[] src= UploadFile.uploadFile(request,file);
        resultData.setData(src);
        return resultData;
    }

    @ResponseBody
    @RequestMapping(value = "/queryRepairByAreaId.do",method = RequestMethod.GET)
    @ApiOperation(value = "根据区域id查询检修厂，前面有查询省市的接口",notes = "根据区域id查询检修厂，前面有查询省市的接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="数据库没有此数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "area_city_id",value = "区域id",required = true,dataType = "int",paramType = "query")
    })
    //根据区域id查询检修厂，前面有查询省市的接口
    public ResultData queryRepairByAreaId(int area_city_id){
        ResultData resultData = new ResultData();
        List<Map> mapList = userKFService.queryRepairByAreaId(area_city_id);
        if (mapList.size()>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("检修厂查询成功");
            resultData.setData(mapList);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("数据库没有此数据");
        }

        return resultData;
    }


    @ResponseBody
    @RequestMapping(value = "/queryUserCouponByUserId.do",method = RequestMethod.GET)
    @ApiOperation(value = "通过用户id查询用户所拥有的优惠劵接口",notes = "通过用户id查询，用户所拥有的优惠劵的接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="您暂时还没有优惠卷")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id",value = "用户id",required = true,dataType = "int",paramType = "query"),
    })
    //通过用户id查询，用户所用于得优惠劵接口
    public ResultData queryUserCouponByUserId(int user_id){
        ResultData resultData = new ResultData();
        List<Map> mapList = userKFService.queryUserCouponByUserId(user_id);
       if (mapList.size()>0){
           resultData.setCode(200);
           resultData.setMsg("查询成功");
           resultData.setData(mapList);
       }else {
           resultData.setCode(500);
           resultData.setMsg("您暂时还没有优惠卷");
       }
        return resultData;
    }


    @ResponseBody
    @RequestMapping(value = "/queryUserOrderAllUnreadByUserId.do",method = RequestMethod.GET)
    @ApiOperation(value = "通过用户id查询所有订单",notes = "通过用户id查询所有订单消息接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParam(name = "user_id",value = "用户id",required = true,paramType = "query")
    //通过用户id查询所有订单
    public ResultData queryUserOrderAllUnreadByUserId(int user_id){
        ResultData resultData = new ResultData();
        resultData=userKFService.queryUserOrderAllUnreadByUserId(user_id);
        return resultData;
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserPwdByUserId.do",method = RequestMethod.GET)
    @ApiOperation(value = "通过用户id查询原始密码是否正确",notes = "通过用户id查询原始密码是否正确消息接口",httpMethod = "get",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=500,message="原始密码错误！！")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id",value = "用户id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "user_pwd",value = "用户原始密码" ,required = true  ,dataType = "String",paramType ="query")
    })
    //通过用户id查询原始密码是否正确
    public ResultData queryUserPwdByUserId(Integer user_id,String user_pwd){

        ResultData resultData = new ResultData();
        String new_pwd = PasswordEncryption.passwordEncryption(user_pwd);
        Map map = userKFService.queryUserPwdByUserId(user_id);
        if (map.get("user_pwd").equals(new_pwd)){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("原始密码正确");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("原始密码不正确");
        }
        return resultData;
    }

    @ResponseBody
    @RequestMapping(value = "/updateUserPwdByUserId.do",method = RequestMethod.POST)
    @ApiOperation(value = "通过用户id修改用户的密码",notes = "通过用户id修改用户的密码接口",httpMethod = "post",response = ResultData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="密码更改成功"),
            @ApiResponse(code=500,message="后台服务异常")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_id",value = "用户id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "user_pwd",value = "用户原始密码" ,required = true  ,dataType = "String",paramType ="query")
    })
    //通过用户id修改用户的密码
    public ResultData updateUserPwdByUserId( int user_id,String user_pwd){
        ResultData resultData = new ResultData();
        System.out.println("=========user_id"+user_id);
        System.out.println("=========user_pwd"+user_pwd);
        int result = userKFService.updateUserPwdByUserId(user_id,user_pwd);
        if (result > 0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("密码更改成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("后台服务异常");
        }
        return resultData;
    }


}








